package com.han.spider.contact;

import com.han.util.UnZipper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

public class TelecomContact {
    private final HttpClient httpClient;
    private final HttpRequest.Builder requestBuilder;

    private TelecomContact() {
        HttpClient.Builder clientBuilder = HttpClient.newBuilder();
        httpClient = clientBuilder.build();

        requestBuilder = HttpRequest.newBuilder()
                .timeout(Duration.ofMinutes(1))
                .setHeader("Accept", "*/*")
                .setHeader("Accept-Encoding", "gzip, deflate")
                .setHeader("Accept-Language", "en,zh-CN;q=0.9,zh;q=0.8")
                .setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .setHeader("Cookie", "mssPortalPreLogin=0; JSESSIONID1=9yl7zD0IXMHtRucAx7iQXUhGnsobu1obFMcGeposmtJvj8s1AKw3!1909238991; MssSsoToken=vWWxAA+dGBQN+8nCDDPrP4DLGeN0a5ycO27wJFC+OPI=")
                .setHeader("DNT", "1")
                .setHeader("Referer", "http://www.ahoa.com:9099/portal/web/af_txl.htm")
                .setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36")
                .setHeader("X-Requested-With", "XMLHttpRequest");
    }

    public static void main(String[] args) {
        TelecomContact telecomContact = new TelecomContact();
        String company = "亳州";
        telecomContact.t(telecomContact.getDireList(company), company);
        company = "蒙城";
        telecomContact.t(telecomContact.getDireList(company), company);
        company = "利辛";
        telecomContact.t(telecomContact.getDireList(company), company);
        company = "涡阳";
        telecomContact.t(telecomContact.getDireList(company), company);
    }

    private List<String> getDireList(String company) {
        if ("亳州".equals(company)) {
            return bozhou();
        } else if ("蒙城".equals(company)) {
            return mengcheng();
        } else if ("利辛".equals(company)) {
            return lixin();
        } else if ("涡阳".equals(company)) {
            return guoyang();
        } else {
            throw new NullPointerException();
        }
    }

    private List<String> bozhou() {

        List<String> direList = new ArrayList<>();
        direList.add("99D2FCB35F914A229A63766C3DBEF113");
        direList.add("90BFD278EA5047C483D2A612AAB27070");
        direList.add("C2262ED0B8F8437EAB350B5B9A7675E5");
        direList.add("70C75F43E4CF4D8EB16ADA13B5805579");
        direList.add("F8E16B703BC647AD91C75AA9189CD3D4");
        direList.add("BBDCAC1FCF3D474D824A19CA2FF29881");
        direList.add("6A8540B5742E4E5EA59DC5FFBA33478F");
        direList.add("224FAA79DF7C4E71B93073F7F852998E");
        direList.add("05857B0B8BC74E7DA204D1FF27E12A8E");
        direList.add("7EEC506EDC274AC6A5603F95224E97C6");
        direList.add("27C5378934BC4AED83176B73F59A154F");
        direList.add("F7323526A0E54947ACD4E7759E4B082D");
        direList.add("29030A39997444F099EE7530CD549CA5");
        direList.add("0FA13A6D244744378E734DCD219D0D90");
        direList.add("B56B71A660B845909B27C3D2856AF33E");
        direList.add("220B0E7F2CAD4740A68C494D41BFC208");
        direList.add("4A59640234844B29BFF5946B95ACD6D9");
        direList.add("297e23523b2a8e08013b879926a139fc");
        direList.add("F87628C2A9784F34B7C56547D6940867");
        direList.add("DE7CC0C22CBF4DD1B575AA535FC6F4E0");
        direList.add("C2E7395E80FC4334BFF6676D0752A45A");
        direList.add("C1C7D8ACF55E4A71A2A9A2AC7DE1600E");
        direList.add("B721D0476E7C41878DB0EEE2296B18E9");
        direList.add("04C5241BB2B8412BB7DD7634E18EA732");
        direList.add("50760A84A39A4BFAB27DBFE4AD58E2B4");
        direList.add("C9C6115339384A36BBA7A89D47CCC57C");
        direList.add("FF689F7BFA554B65B2DC1F2838029548");
        direList.add("24A9C387B0DE445DB65DF73001A94C34");
        direList.add("D2F92863BBFE4599BB660A03360EE00B");
        direList.add("297e23523a641e06013b25cc88a756e5");
        direList.add("F7B4B72AB12C41F19393A5D0A1D31A46");
        direList.add("31DC26B54D904A2A8C323CE2480F3942");
        direList.add("03140D56D2924E1C93A7F8BB7F6D0BCD");
        return direList;
    }

    private List<String> mengcheng() {

        List<String> direList = new ArrayList<>();
        direList.add("AC66B58DD5304F8C9C4E63840939D7D3");
        return direList;
    }

    private List<String> lixin() {

        List<String> direList = new ArrayList<>();
        direList.add("392E563B5C1B4C78A71DB353A4938406");
        return direList;
    }

    private List<String> guoyang() {

        List<String> direList = new ArrayList<>();
        direList.add("CB0734B8006447CBB7440BB170747E55");
        return direList;
    }

    private void t(List<String> direList, String company) {
        List<Contact> contactList = new ArrayList<>();
        for (int i = 0; i < direList.size(); i++) {
            String superId = direList.get(i);
            try {
                List<String> tempDireList = getMenu(superId);
                List<Contact> tempContactList = getContactList(company, superId);
                contactList.addAll(tempContactList);
                direList.addAll(tempDireList);
            } catch (IOException | InterruptedException e) {
                System.out.println(superId + "获取层级菜单异常");
            }
        }
        for (Contact contact : contactList) {
            System.out.println(contact);
        }
    }

    private List<String> getMenu(String superId) throws IOException, InterruptedException {

        String url = "http://www.ahoa.com:9099/portal/ims/org/bookTreedata.htm?supId=" + superId + "&start=0";
        HttpRequest httpRequest = requestBuilder
                .uri(URI.create(url))
                .build();

        HttpResponse<byte[]> byteResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofByteArray());
        String backMsg = new String(UnZipper.unGzipByBytes(byteResponse.body()), StandardCharsets.UTF_8);
        JSONObject jsonObject = new JSONObject(backMsg);
        JSONArray items = jsonObject.getJSONArray("items");
        List<String> superIdList = new ArrayList<>();
        for (int i = 0; i < items.length(); i++) {
            JSONObject item = items.getJSONObject(i);
            String id = item.getString("id");
            superIdList.add(id);
        }

        return superIdList;
    }

    private List<Contact> getContactList(String dire, String superId) throws IOException, InterruptedException {
        String url = "http://www.ahoa.com:9099/portal/ims/addressBook/listUser4Main.htm?pageDisable=1&parentId=" + superId + "&dojo.preventCache=1586856102963";
        HttpRequest httpRequest = requestBuilder
                .uri(URI.create(url))
                .build();

        List<Contact> contactList = new ArrayList<>();

        HttpResponse<byte[]> byteResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofByteArray());
        String backMsg = new String(UnZipper.unGzipByBytes(byteResponse.body()), StandardCharsets.UTF_8);
        JSONObject jsonObject = new JSONObject(backMsg);
        JSONArray items = jsonObject.getJSONArray("items");
        for (int i = 0; i < items.length(); i++) {
            JSONObject item = items.getJSONObject(i);
            try {
                String name = item.getString("realName");
                String organizaiton = item.getString("organizaiton");
                String post = item.getString("post");
                String mobilePhone = item.getString("mobilePhone");
                if (mobilePhone.isBlank() || mobilePhone.isEmpty()) {
                    continue;
                }
                if ("部门经理".equals(post)) {
                    post = "主任";
                } else if ("部门副经理".equals(post)) {
                    post = "副主任";
                }
                Contact contact = new Contact(name, dire + "电信", organizaiton + post, mobilePhone);
                contactList.add(contact);
            } catch (JSONException e) {
                System.out.println(item);
            }
        }
        return contactList;
    }
}
